package com.seanLab.dao;

import com.google.gson.Gson;
import com.seanLab.domain.Article;
import com.seanLab.domain.Image;

import java.util.Collection;
import java.util.List;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;

import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;

import org.bson.Document;
import java.util.Arrays;
import com.mongodb.Block;

import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Updates.*;
import com.mongodb.client.result.UpdateResult;

import javax.print.Doc;
import java.util.ArrayList;

/**
 * Created by ailly on 17-10-9.
 */
public class MongoImageDaoImpl implements ImageDao {
    private MongoClient mongoClient;
    private MongoDatabase database;
    private MongoCollection<Document> collection;

    MongoCursor<Document> cursor;

    public MongoImageDaoImpl() {
        mongoClient = new MongoClient( "10.141.211.93" , 27017 );
        database = mongoClient.getDatabase("chens");
        collection = database.getCollection("article_1259");
    }

    public MongoCollection<Document> getCollection() {
        return collection;
    }

    public List<Article> getAll() {
        List<Article> articles = new ArrayList<>();
        Gson gson = new Gson();

        cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
                Document d = cursor.next();
                Article tmpArticle = gson.fromJson(d.toJson(), Article.class);
                tmpArticle.setArticleID(d.getString("_id"));
                articles.add(tmpArticle);
            }
        } finally {
            cursor.close();
        }

        return articles;
    }

    public Article getOne() {
        Gson gson = new Gson();
        Document doc = collection.find().first();
        Article tmpArticle = gson.fromJson(doc.toJson(), Article.class);
        tmpArticle.setArticleID(doc.getString("_id"));
        return tmpArticle;
    }

    private Article mapDocToArticle(Document doc){
        Article tmpArticle = new Article();
        tmpArticle.setArticleID(doc.getString("_id"));
        return tmpArticle;
    }

    @Override
    public int insertImage(Image image) {
        return 0;
    }

    @Override
    public int insertImageCollection(List<Image> imageList) {
        return 0;
    }

    @Override
    public int updateImageCollection(List<Image> imageList) {
        return 0;
    }

    @Override
    public int removeImageById(String id) {
        return 0;
    }

    @Override
    public int updateImageById(String id) {
        return 0;
    }

    @Override
    public Collection<Image> getAllImages() {

        return null;
    }

    @Override
    public Image getImageById(String id) {
        return null;
    }
}
